
package dao;

import entities.Car;
import entities.Customer;
import entities.Reservation;
import enums.StatusEnum;
import hibernateUtil.HibernateSessionFactory;
import java.util.ArrayList;
import java.util.Date;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Query;
import org.hibernate.Transaction;
import org.hibernate.classic.Session;


public class ReservationDao {
    private static final Log log = LogFactory.getLog(ReservationDao.class);
    
    public int getReservationCountByRepairDate(Date repairDate){
        Session session = null;
        Transaction transaction = null;
        log.debug("getting Reservation instance with repairDate: " + repairDate);   
        try{
            session = HibernateSessionFactory.getSessionFactory().openSession();
            transaction = session.beginTransaction();
            Query query = session.createQuery("from Reservation where repairDate = :repairDate and reservation_status_id = :status");
            query.setParameter("repairDate", repairDate); 
            query.setParameter("status", new StatusDao().getByCode( StatusEnum.ACC).getStatusId()); 
            return query.list().size();
        } catch (RuntimeException re){
            	log.error("get failed", re);
		throw re;
        } finally {
            transaction.commit();
            session.close();
        }        
    }
    
    public int saveReservation(Reservation reservation){
        org.hibernate.Session session = null;
        Transaction transaction = null;
        log.debug("persisting Reservation instance");
        try {
            session = HibernateSessionFactory.getSessionFactory().openSession();
            transaction = session.beginTransaction();
            session.save(reservation);
            log.debug("save successful");
            return 1;
	} catch (RuntimeException re) {
            log.error("save failed", re);
            throw re;
	}
        finally{
            transaction.commit();
            session.close();
        }          
    }
    
    public ArrayList<Reservation> getAllReservationData(){
        org.hibernate.Session session = null;
        Transaction transaction = null;
        ArrayList<Reservation> reservationList = null;
        log.debug("getting Reservation List");
        try {
            session = HibernateSessionFactory.getSessionFactory().openSession();
            transaction = session.beginTransaction();
            reservationList = (ArrayList<Reservation>) session.createCriteria(Reservation.class).list();
            if(reservationList.isEmpty()){
                log.debug("get successful, empty carList");
            } else {
                log.debug("get successful, not empty carList");
            }
        } catch (RuntimeException re) {
              log.error("get failed", re);
              throw re;
        } finally {
            transaction.commit();
            session.close();
        }
        return reservationList;        
    }
    
    public Reservation getReservationById(int reservationId){
        org.hibernate.Session session = null;
        Transaction transaction = null;
        log.debug("getting Reservation instance with id: " + reservationId);
        try{
            session = HibernateSessionFactory.getSessionFactory().openSession();
            transaction = session.beginTransaction();
            Reservation instance = (Reservation) session.get(Reservation.class, reservationId);
            if (instance == null) {
                log.debug("get successful, no instance found");
            } else {
		log.debug("get successful, instance found");
            }
		return instance;
        } catch (RuntimeException re){
            	log.error("get failed", re);
		throw re;
        } finally {
            transaction.commit();
            session.close();
        }        
    }
    
    public void update(Reservation reservation){
        org.hibernate.Session session = null;
        Transaction transaction = null;
        log.debug("update Reservation");
        try {
            session = HibernateSessionFactory.getSessionFactory().openSession();
            transaction = session.beginTransaction();
            session.update(reservation);
            log.debug("update successful");
	} catch (RuntimeException re) {
            log.error("update failed", re);
            throw re;
	}
        finally{
            transaction.commit();
            session.close();
        }        
    }    
}
